﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TimViec.backend
{
    public partial class QuanLyHocVan : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Admin"] == null)
            {
                Response.Redirect("/quan-tri/login.html");
            }
            if (!IsPostBack)
            {
                LoadData();
            }
        }
        public void LoadData()
        {
            rptHocVan.DataSource = BLL.Admin.HocVanHelper.List_DMHoVan();
            rptHocVan.DataBind();
        }
        protected void btSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtHocVan.Text))
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Tên học vấn không được để trống!');", true);
                return;
            }
            BLL.Admin.HocVanHelper.Add_DanhMucHocVan(txtHocVan.Text);
            txtHocVan.Text = "";
            LoadData();
        }

        protected void btUpdate_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtHocVan.Text))
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Tên học vấn không được để trống!');", true);
                return;
            }
            BLL.Admin.HocVanHelper.Update_DMHocVan(int.Parse(lbCapBacId.Text), txtHocVan.Text);
            btSave.Visible = true;
            btUpdate.Visible = false;
            txtHocVan.Text = "";
            LoadData();
        }

        protected void btCancel_Click(object sender, EventArgs e)
        {
            btSave.Visible = true;
            btUpdate.Visible = false;
            txtHocVan.Text = "";
        }

        protected void rptHocVan_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                int hocvanid = int.Parse(e.CommandArgument.ToString());
                lbCapBacId.Text = hocvanid.ToString();
                DAL.tbl_DM_HocVan hocVan = BLL.Admin.HocVanHelper.Get_DMHocVan(hocvanid);
                if (hocVan != null)
                {
                    txtHocVan.Text = hocVan.Mota;
                    btSave.Visible = false;
                    btUpdate.Visible = true;
                }
            }
        }
    }
}